package com.bprogram.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bprogram.pojo.Comment;
import com.bprogram.pojo.History;
import com.bprogram.service.HistoryService;
import com.bprogram.utils.RedisUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author cai
 * @since 2023-11-21
 */
@RestController
@RequestMapping("/history")
public class HistoryController {
    @Resource
    private HistoryService historyService;

    @Resource
    private UserFeign userFeign;
    @Resource
    private RedisUtil redisUtil;
    /**
     * 查找当前登录用户的历史记录
     *
     * @param currentPage
     * @param pageSize
     * @return
     */
    @PostMapping("/getHistoryListByUserId")
    public IPage<History> getHistoryListCondition(String currentPage, String pageSize) {
        return historyService.getHistoryListCondition(currentPage, pageSize);
    }

    @PostMapping("/saveOrUpdateHistory")
    public boolean saveOrUpdateHistory(History history) {

        return historyService.saveOrUpdateHistory(history);
    }


    /**
     * 删除当前登录对象的历史记录
     */
    @GetMapping("/deleteByCreateId")
    public void deleteByCreateId() {
        String userId = userFeign.getLoginUser().getUserId()+"";
        QueryWrapper<History> wrapper = new QueryWrapper<>();
        wrapper.eq("createId", userFeign.getLoginUser().getUserId());
        List<History> historyList = historyService.list(wrapper);
        for (History history : historyList) {
            redisUtil.setRemove(userId,history.getVideoId()+"");
        }

        historyService.remove(wrapper);
    }

    /**
     *
     * @param videoTitle
     * @return
     */
    @GetMapping("/getHistoryByVideoTitle")
    public List<History> getHistoryByVideoTitle(String videoTitle){
        QueryWrapper<History> wrapper = new QueryWrapper<>();
        wrapper.like("videoTitle",videoTitle);
        List<History> historyList = historyService.list(wrapper);
        return historyList;
    }



}

